/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart;

import org.eclipse.gef.EditPart;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.figures.StringFigure;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Controler for the label in charge of the applied stereotypes list for a
 * element represented by a link.
 */
public class StereotypeDisplayEditPart extends LinkNameDisplayEditPart{
	
	/**
	 * The element type accepted by the associated list model.
	 * If null, any type is accepted.
	 */
	protected int channel=14;
	
	/**
	 * the owner of this edit part.
	 */
	protected GraphEdgeEditPart parentEditPart;

	/**
	 * Create a FeatureList EditPart accepting specified type from drag and drop.
	 * Create a ListEditPart for a List accepting only specified element.
	 * 
	 * @param element 
	 * @param elementType 	 * @param modelManager 
	 * @param graphnode 
	 * @param parent 
	 */
	public StereotypeDisplayEditPart(GraphNode graphnode,org.eclipse.uml2.uml.Element element,ModelManager modelManager, GraphEdgeEditPart parent) {
		super(graphnode,element,modelManager);
		//super(graphnode,modelManager);
		Debug.debug(this,"create StereotypeDisplayEditPart("+ element+")",channel);
		parentEditPart = parent;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.NameDisplayEditPart#createFigure()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected StringFigure createFigure() {
		String text = getTextToDisplay();
		return new StringFigure(text);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getFigure()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public StringFigure getFigure() {
		return (StringFigure)super.getFigure();
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.LinkNameDisplayEditPart#refreshVisuals()
	 */
	/**
	 * 
	 */
	@Override
	protected void refreshVisuals() {
		// display text representing applied steretoypes
		String stereotypes = getTextToDisplay();
		getFigure().setText(stereotypes);
		getFigure().setVisible(this.isVisible());
	}
	
	/**
	 * Sets the parent edit part (the edge edit part reprensenting the
	 * stereotyped element).
	 * 
	 * @param parent 
	 */
	public void setParentEditPart(GraphEdgeEditPart parent) {
		parentEditPart = parent;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#getParent()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public EditPart getParent() {
		return parentEditPart;
	}
	
	/**
	 * Returns the String to be displayed in the label.
	 * 
	 * @return the String to be displayed in the label
	 */
	protected String getTextToDisplay() {
		String stereotypes = "";
		if(parentEditPart!=null){
			if(parentEditPart.getGraphEdge().getSemanticModel().getPresentation().equals(ModelerHelper.STEREOTYPE_TEXT_VERTICAL_PRESENTATION)) {
				stereotypes = parentEditPart.verticalStereotypesToDisplay();
			} else {
				stereotypes = parentEditPart.horizontalStereotypesToDisplay();
			}

			if(stereotypes!=""){//stereotype exists
				return PapyrusPlugin.ST_LEFT+stereotypes+PapyrusPlugin.ST_RIGHT;
			}
		}
		return "";
	}
	
}
